package com.czy.tank.pattern.strategy.fire;

import com.czy.tank.common.Group;
import com.czy.tank.config.ResourceMgr;
import com.czy.tank.game.Audio;
import com.czy.tank.game.Bullet;
import com.czy.tank.game.Tank;
import com.czy.tank.game.TankFrame;

/********************************************************************
 * @brief   默认开火模式
 * @version 0.1
 * @date 2022年09月01日 21:22
 * @author changziyang
 ********************************************************************/
public class DefaultFireStrategy implements FireStrategy{

    public static volatile DefaultFireStrategy INSTANCE;

    private DefaultFireStrategy(){}

    public static DefaultFireStrategy getInstance(){
        if (INSTANCE != null){
            return INSTANCE;
        }
        synchronized (DefaultFireStrategy.class){
            if (INSTANCE == null){
                INSTANCE = new DefaultFireStrategy();
            }
        }
        return INSTANCE;
    }

    @Override
    public void fire(Tank tank) {
        /*根据坦克方向 计算子弹打出的位置*/
        int bulletX = tank.x + tank.WIDTH/2 - ResourceMgr.bulletU.getWidth()/2;
        int bulletY = tank.y + tank.HEIGHT/2 - ResourceMgr.bulletU.getHeight()/2;
        new Bullet(bulletX,bulletY,tank.dir, TankFrame.getInstance(),tank.getGroup());
        if(tank.getGroup() == Group.GOOD) {
            /*播放主战坦克开火音效*/
            new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
        }
    }
}
